﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.ComponentModel;

namespace ESpace
{
    public class EUdpSend : Component
    {
        UdpClient client;        
        String RemAddr = "127.0.0.1";
        int RemPort = 1234;
        //int pLocalPort = 1234;

        public String RemoteAddr
        {
            get { return RemAddr; }
            set
            {
                IPAddress a;
                if (IPAddress.TryParse(value, out a))
                    RemAddr = value;
            }
        }

        public int RemotePort
        {
            get { return RemPort; }
            set { if (value > 0 && value < 65535)RemPort = value; }
        }

        /// Конструктор
        public EUdpSend()
        {
            /*if (pLocalPort == 0)*/ client = new UdpClient();
            //else client = new UdpClient(pLocalPort);
           
        }

        public int SendData(byte[] mas)
        {
            return SendData(mas, RemAddr, RemPort);
        }

        public int SendData(byte[] mas, String ip, int port)
        {
            try
            {
                return client.Send(mas, mas.Length, ip, port);
            }
            catch { return -1; }
        }

    }
}
